package cn.tom.org.controller;

import cn.tom.basic.util.AjaxResult;
import cn.tom.org.pojo.domain.Employee;
import cn.tom.org.pojo.query.EmployeeQuery;
import cn.tom.org.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工模块")
public class EmployeeController {

    @Autowired
    private EmployeeService service;

    @GetMapping("/{id}")
    public Employee getOne(@PathVariable("id")Long id) {
        return service.getOne(id);
    }

    @GetMapping
    public List<Employee> getAll() {
        return service.getAll();
    }

    @PutMapping
    public AjaxResult save(@RequestBody Employee employee) {
        if(employee.getId() == null){
            service.add(employee);
            return AjaxResult.ok();
        }else{
            service.update(employee);
            return AjaxResult.ok();
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id) {
        service.delete(id);
        return AjaxResult.ok();
    }

    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        service.patchDelete(ids);
        return AjaxResult.ok();
    }

    @PostMapping
    public PageInfo<Employee> queryPage(@RequestBody EmployeeQuery query) {
        return service.queryPage(query);
    }
}
